package de.global42.clubapp.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

/**
 * @author lutz
 * 
 */
@Entity
public class ClientMission implements Serializable{

	/**
	 * Generated Version UID
	 */
	private static final long serialVersionUID = -4197817481264109092L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id", updatable = false, nullable = false)
	private Long id = null;

	@Version
	@Column(name = "version")
	private int version = 0;

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "mission_id")
	@NotNull
	protected Mission mission;

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "client_id")
	@NotNull
	protected Client client;

	@OneToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
	protected Set<ValidationCode> codes;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn
	protected MissionStatus missionStatus;
	
	@OneToOne(optional = true, fetch = FetchType.LAZY)
	protected ClientCoupon clientCoupon;

	public Long getId() {
		return this.id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public int getVersion() {
		return this.version;
	}

	public void setVersion(final int version) {
		this.version = version;
	}

	public Mission getMission() {
		return mission;
	}

	public void setMission(Mission mission) {
		this.mission = mission;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Set<ValidationCode> getCodes() {
		return codes;
	}

	public void setCodes(Set<ValidationCode> codes) {
		this.codes = codes;
	}

	public MissionStatus getMissionStatus() {
		return missionStatus;
	}

	public void setMissionStatus(MissionStatus missionStatus) {
		this.missionStatus = missionStatus;
	}

	public ClientCoupon getClientCoupon() {
		return clientCoupon;
	}

	public void setClientCoupon(ClientCoupon clientCoupon) {
		this.clientCoupon = clientCoupon;
	}

	public String toString() {
		String result = "";
		if (id != null)
			result += id;
		return result;
	}

	@Override
	public boolean equals(Object that) {
		if (this == that) {
			return true;
		}
		if (that == null) {
			return false;
		}
		if (getClass() != that.getClass()) {
			return false;
		}
		if (id != null) {
			return id.equals(((ClientMission) that).id);
		}
		return super.equals(that);
	}

	@Override
	public int hashCode() {
		if (id != null) {
			return id.hashCode();
		}
		return super.hashCode();
	}

	public boolean isComplete() {
		return mission.getTarget().fulfilledBy(missionStatus);
	}
}